﻿function PageLoad() {
    $("#txtPassword").val($("#TempPassword").val());
}
function Login() {
    var param;
    var username = document.getElementById("txtUserName").value;
    var password = document.getElementById("txtPassword").value;
    if (username.length <= 0) {
        alert("用户名不能为空！");
        return false;
    }
    document.getElementById("btnLogin").disabled = true;
    param = 'UserName=' + encodeURIComponent(username) + '&Password=' + password + '&ServerAddress=' + document.all.h_rtxserverip.value; +'&ServerPort=' + document.all.h_rtxserverport.value;

    var httpRequest = new HttpRequest();
    httpRequest.targetUrl = '/inside/ajaxLogin.aspx';
    httpRequest.method = 'get';
    httpRequest.param = param;
    httpRequest.callBack = GetLoginComplete;
    httpRequest.Send();
}

function GetLoginComplete(status, html) {
    //alert(status);
    document.getElementById("btnLogin").disabled = false; 
     if (status == 4) {
         //alert(status);
        var d = eval("(" + html + ")");
        if (d.ResultCode != undefined) {
            if (d.ResultCode == 0) {
                var hdnUrl = document.getElementById("hdnUrl").value;

                toUrl = hdnUrl == "" ? "/inside/index.aspx" : hdnUrl; //正常情况下
                loginRTX();
                setTimeout(function () { window.location.href = toUrl; }, 800);                
            } else {
                var obj;
                if (d.ResultCode == "1") {
                    document.getElementById("txtUserName").select();
                    document.getElementById("txtPassword").val("");
                } else if (d.ResultCode == "2") {
                    obj = document.getElementById("txtPassword");
                    obj.value = "";
                    obj.focus();
                }
                $("#spMessage").html(d.Message);  
                //document.getElementById("spMessage").innerHtml = d.Message;             
            }
        }
    }
}

function loginRTX() {
    try {
        if (document.all.h_rtxinstalled.value == "1") {
            var _oadomain = "/" + document.all.hdOADomain.value;
            var username = document.getElementById("txtUserName").value;
            var url = _oadomain + '/main/rtxlogin.aspx?UserName=' + encodeURIComponent(username);
            window.open(url, '_blank', "");
//            var obj1 = new ActiveXObject("rtxclient.rtxapi");
//            var objProp = obj1.GetObject("Property");
//            objProp.value("RTXUsername") = document.all.txtUserName.value;
//            objProp.value("LoginSessionKey") = loginSessionKey;
//            objProp.value("ServerAddress") = document.all.h_rtxserverip.value;
//            objProp.value("ServerPort") = document.all.h_rtxserverport.value;
//            obj1.call(2, objProp);
        }
    }
    catch (e) {
        //alert(e);
    }
}

/*设置与获取Cookie*/
var Cookie = {}
Cookie.write = function (key, value, duration) {
    var d = new Date();
    d.setTime(d.getTime() + 1000 * 60 * 10);
    document.cookie = key + "=" + encodeURI(value) + "; expires=" + d.toGMTString();
};
Cookie.read = function (key) {
    var arr = document.cookie.match(new RegExp("(^| )" + key + "=([^;]*)(;|$)"));
    if (arr != null)
        return decodeURIComponent(arr[2]);
    return "";
};

function onMemberUP(me) {
    if (me.checked) {
        Cookie.write("un", document.getElementById("txtUserName").value, 30);
        Cookie.write("pd", document.getElementById("txtPassword").value, 30);
        Cookie.write("userpwd", "1", 30);
    } else {
        Cookie.write("un", "", 30);
        Cookie.write("pd", "", 30);
        Cookie.write("userpwd", "", 30);
    }
}
